package sg.edu.nus.comp.icreate.ivletalk.module;

import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;

import sg.edu.nus.comp.icreate.ivletalk.forum.Forum;
import sg.edu.nus.comp.icreate.ivletalk.utils.xml.AbstractXmlParser;
import sg.edu.nus.comp.icreate.ivletalk.workbin.Workbin;

public class ModuleElementXmlParser extends AbstractXmlParser {

	private static final String TAG_FORUM = "Data_Forum_Tool";
	private static final String TAG_WORKBIN = "Data_Workbin_Tool";

	@Override
	public Object getResult() {
		int eventType;
		Map<ElementType, Element> elements = new HashMap<ElementType, Element>();
		// Trivial Initialization, useless.
		Element currentElement = null;
		try {
			goToTag(TAG_MODULE, XmlPullParser.START_TAG);
			do {
				eventType = parser.getEventType();
				if (eventType == XmlPullParser.START_TAG && TAG_FORUM.equals(parser.getName())) {
					currentElement = new Forum();
					elements.put(ElementType.FORUM, currentElement);
				} else if (eventType == XmlPullParser.START_TAG && TAG_WORKBIN.equals(parser.getName())) {
					currentElement = new Workbin();
					elements.put(ElementType.WORKBIN, currentElement);
				} else if (eventType == XmlPullParser.START_TAG && TAG_ID.equals(parser.getName())) {
					parser.next();
					if (currentElement != null) {
						currentElement.setId(parser.getText());
						currentElement = null;
					}
				}
				parser.next();
			} while (parser.getEventType() != XmlPullParser.END_TAG || !TAG_MODULE.equals(parser.getName()));

		} catch (Exception e) {
			Log.e("error", "Error in parse xml", e);
		}
		return elements;
	}

}
